package cn.wolfcode.wolf2w.controller;


import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/strategies")
public class StrategyController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;


    @GetMapping("/detail")
    public Object detail(String id) {
        strategyStatisVORedisService.viewnumIncrease(id);
        return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/query")
    public Object query(@ModelAttribute("qo") StrategyQueryObject qo) {
        return JsonResult.success(strategyService.query(qo));
    }

    @GetMapping("/themes")
    public Object themes() {
        return JsonResult.success(strategyThemeService.list());
    }

    @GetMapping("/comments")
    public Object comments(@ModelAttribute("qo") StrategyCommentQueryObject queryObject) {
        strategyCommentService.query(queryObject);

        return JsonResult.success(strategyCommentService.query(queryObject));
    }

    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(@UserParam UserInfo userInfo, StrategyComment strategyComment) {
        BeanUtils.copyProperties(userInfo, strategyComment);
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.save(strategyComment);

        strategyStatisVORedisService.commentNumIncrease(strategyComment.getStrategyId());
        return JsonResult.success();

    }

    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(@UserParam UserInfo userInfo, String cid) {
        strategyCommentService.commentThumb(userInfo.getId(), cid);
        return JsonResult.success();

    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        StrategyStatisVO strategyStatisVO = strategyStatisVORedisService.getToken(sid);
        return JsonResult.success(strategyStatisVO);
    }

    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {
        return JsonResult.success(strategyStatisVORedisService.thumbsupNumIncrease(sid, userInfo));
    }

    @PostMapping("/favor")
    @RequireLogin
    public Object favor(String sid, @UserParam UserInfo userInfo) {
        return JsonResult.success(strategyStatisVORedisService.favorNumIncrease(sid, userInfo));
    }

}
